package com.moxiu.browser;

import org.w3c.dom.Text;

import com.moxiu.browser.provider.BrowserContract;
import com.moxiu.launcher.R;

import android.app.Activity;
import android.content.ContentUris;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class EditBookMarkHistoryActivity extends Activity implements OnClickListener{
	private static final String TAG = "EditBookMarkHistoryActivity";
	
	private TextView backTv;
	private EditText bookmarkHistoryNameEv;
	private EditText bookmarkHistoryUrlEv;
	private Button saveEditBtn;
	
	private Bundle bundle;
	private ContentValues values;
	private int bookmarkHistoryId;
	private String bookmarkHistoryName;
	private String bookmarkHistoryUrl;

	public EditBookMarkHistoryActivity() {
		// TODO Auto-generated constructor stub
	}
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		try {
			requestWindowFeature(Window.FEATURE_NO_TITLE);
		} catch (Exception e) {
		}
		setContentView(R.layout.layout_edit_bookmark_history);
		initData();
		init();
	}
	
	/*
	 * 获取数据
	 */
	private void initData() {
		try {
			bundle = getIntent().getExtras();
			bookmarkHistoryId = Integer.parseInt(bundle.getString("bookmarkId"));
			bookmarkHistoryName= bundle.getString("bookmarkName");
			bookmarkHistoryUrl = bundle.getString("bookmarkUrl");
		} catch (Exception e) {
			bookmarkHistoryId = 0;
			bookmarkHistoryName="";
			bookmarkHistoryUrl ="";
		}
	}

	/*
	 * 布局初始化
	 */
	void init(){
		backTv = (TextView) findViewById(R.id.back_edit_tv);
		bookmarkHistoryNameEv = (EditText) findViewById(R.id.bookmark_history_name_ev);
		bookmarkHistoryUrlEv = (EditText) findViewById(R.id.bookmark_history_url_ev);
		saveEditBtn = (Button) findViewById(R.id.save_edit_bookmark_history_btn);
		
		bookmarkHistoryNameEv.setText(bookmarkHistoryName);
		bookmarkHistoryUrlEv.setText(bookmarkHistoryUrl);
		bookmarkHistoryNameEv.setSelection(bookmarkHistoryName.length());
		bookmarkHistoryNameEv.setFocusable(true);
		saveEditBtn.setOnClickListener(this);
		backTv.setOnClickListener(this);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.save_edit_bookmark_history_btn:
			getEditValue();
			Uri uri = ContentUris.withAppendedId(BookmarkUtils.getBookmarksUri(getApplicationContext()), bookmarkHistoryId);
			int count = getApplicationContext().getContentResolver().update(uri, values, null, null);
			if (count > 0) {
				Toast.makeText(getApplicationContext(), "编辑成功", Toast.LENGTH_SHORT).show();
			}else {
				Toast.makeText(getApplicationContext(), "编辑失败", Toast.LENGTH_SHORT).show();
			}
			finish();
			break;
			
		case R.id.back_edit_tv:
			finish();
			break;

		default:
			break;
		}
	}

	/*
	 * 获取修改后的数据
	 */
	private void getEditValue() {
		values = new ContentValues();
		values.put(BrowserContract.Bookmarks.TITLE, bookmarkHistoryNameEv.getText().toString());
		values.put(BrowserContract.Bookmarks.URL, bookmarkHistoryUrlEv.getText().toString());
	}

}
